﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using MeizuSDK.Drawing;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 定义屏幕相关的属性与函数的类
    /// </summary>
    public static class Screen
    {
        #region DllImport
        [DllImport("PlatformAPIWrapper.dll")]
        private static extern void CommonAPIMzGetWorkArea(ref RECT rect);

        /// <summary>
        /// 转屏之前，调用该API用来通知Shell准备转屏了。为了防止任务栏动画不美观 
        /// </summary>
        /// <returns></returns>
        [DllImport("PlatformAPIWrapper.dll", SetLastError = true, EntryPoint = "ShellNotifyMsgMzBeginScreenRotate")]
        private static extern bool MzBeginScreenRotate();

        /// <summary>
        /// 转屏
        /// </summary>
        /// <returns></returns>
        [DllImport("PlatformAPIWrapper.dll", SetLastError = true, EntryPoint = "ShellNotifyMsgMzChangeDisplaySettingsEx")]
        private static extern bool MzChangeDisplaySettingsEx(uint dwDmdo, bool bFullScreen);
        #endregion

        #region 静态属性
        /// <summary>
        /// 获取表示工作区域的矩形。
        /// </summary>
        public static Rectangle WorkArea
        {
            get{
                RECT rect = new RECT();

                CommonAPIMzGetWorkArea(ref rect);

                return rect.ToRectangle();
            }
        }
        /// <summary>
        /// 获取屏幕宽度
        /// </summary>
        public static int Width
        {
            get
            {
                return System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
            }
        }
        /// <summary>
        /// 获取屏幕高度
        /// </summary>
        public static int Height
        {
            get
            {
                return System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            }
        }
        /// <summary>
        /// 获取客户区域宽度
        /// </summary>
        public static int ClientWidth
        {
            get
            {
                return WorkArea.Width;
            }
        }
        /// <summary>
        /// 获取客户区域高度
        /// </summary>
        public static int ClientHeight
        {
            get
            {
                if (Application.IsShowTaskBar)
                    return Height - TaskBarHeight;
                else
                    return Height;
            }
        }
        /// <summary>
        /// 获取任务栏高度
        /// </summary>
        public static int TaskBarHeight
        {
            get { return MzMetrics.MZM_HEIGHT_TASKBAR; }
        }
        #endregion

        #region 静态函数
        /// <summary>
        /// 旋转屏幕
        /// </summary>
        /// <param name="angle">屏幕旋转角度</param>
        /// <returns>true，表示旋转成功；否则表示旋转失败</returns>
        public static bool Rotate(ScreenRotationAngle angle)
        {
            return Rotate(angle, false);
        }
        /// <summary>
        /// 旋转屏幕
        /// </summary>
        /// <param name="angle">屏幕旋转角度</param>
        /// <param name="fullScreen">是否需要全屏显示</param>
        /// <returns>true，表示旋转成功；否则表示旋转失败</returns>
        public static bool Rotate(ScreenRotationAngle angle, bool fullScreen)
        {
            bool result = MzBeginScreenRotate();

            if (result)
            {
                return MzChangeDisplaySettingsEx((uint)angle, fullScreen);
            }

            return result;
        }
        #endregion
    }
}
